<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div class="head-logo">
        <div class="logo">
          <img
            src="https://storage.lynnn.cn/assets/markdown/public/pictures/2021/08/884621c718a92a61041d3bf0a8d88a2ed1adc95c.jpeg?sign=f08db39f46c5c5800eb59e783a1a8465&t=61285be1"
          />
        </div>
        <span class="headName">电商后台管理系统</span>
      </div>
      <el-button type="info" @click="signOut" size="small">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="togglebutton">|||</div>
        <!-- 侧边栏菜单效果 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="saveS"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="(item, index) in menusList"
            :key="item.id"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 一级菜单的图标 -->
              <i :class="icon[index]"></i>&nbsp;
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + date.path"
              v-for="date in item.children"
              :key="date.id"
              style="width:100%"
              @click="saveState('/' + date.path)"
            >
              <i class="el-icon-menu"></i>
              <span>{{ date.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边主体 -->
      <el-main>
        <!-- 占位符-->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created() {
    this.getMenuList();
  },
  data() {
    return {
      menusList: [],
      icon: [
        "iconfont icon-users",
        "iconfont icon-tijikongjian",
        "iconfont icon-shangpin",
        "iconfont icon-danju",
        "iconfont icon-baobiao",
      ],
      isCollapse: false,
      saveS: "",
    };
  },
  methods: {
    signOut() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status != 200) return this.$message.error(res.mate.msg);
      this.menusList = res.data;
    },
    //点击按钮 切换菜单折叠与展开
    togglebutton() {
      this.isCollapse = !this.isCollapse;
    },
    //保存链接的激活状态
    saveState(el) {
      this.saveS = el;
    },
  },
};
</script>

<style scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
}
.logo {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  align-items: center;
}
.head-logo {
  display: flex;
  justify-content: left;
  align-items: center;
}
.logo img {
  width: 100%;
}
.el-menu {
  border-right: solid 0px #e6e6e6 !important;
}
.headName {
  margin-left: 10px;
  align-items: center;
  color: #fff;
}

.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button {
  background-color: #4a5064;
  color: #fff;
  font-size: 12px;
  text-align: center;
  line-height: 24px;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
